﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;

public partial class EditUser : System.Web.UI.Page
{
    /// <summary>
    /// 要编辑的用户信息
    /// </summary>
    private User user = null;

    /// <summary>
    /// 用户操作对象
    /// </summary>
    private UserManageDAO userDAO = new UserManageDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }  
    }
    /// <summary>
    /// 初始化用户信息
    /// </summary>
    private void BindData()
    {
        String name = HttpUtility.UrlDecode((String)Request["name"]); 
        this.user = this.userDAO.GetUserByName(name);
        if (null == this.user)
        {
            Response.Redirect(GlobalUser.LISTUSER_PAGE);
        }
        //设置用户名文本框
        this.unameTextBox.Text = this.user.name;

        //设置密码文本框
        this.upwdTextBox.Text = this.user.password;

        //设置IP文本框
        this.IPTextBox.Text = this.user.ipLimit;

        //设置功能模块
        this.SetFunctions();

        //设置用户角色
        this.SetRole();
    }

    /// <summary>
    /// 设置所有功能模块
    /// </summary>
    private void SetFunctions()
    {
        IList<FunctionModular> list = this.user.grantedModulars;

        if (list == null)
        {
            return;
        }
        //清除所有模块选择项
        ClearFunctions();

        foreach (FunctionModular temp in list)
        {
            SetFuncion(temp);
        }
    }

    /// <summary>
    /// 清空功能模块选择项
    /// </summary>
    private void ClearFunctions()
    {
        //中文期刊
        this.zwqkCheckBox.Checked = false;
        //外文期刊
        this.wwqkCheckBox.Checked = false;
        //中文会议
        this.zwhyCheckBox.Checked = false;
        //外文会议
        this.wwhyCheckBox.Checked = false;
        //国电法规
        this.gdfgCheckBox.Checked = false;
        //国家标准
        this.gjbzCheckBox.Checked = false;
        //国际标准
        this.gjibzcheckBox.Checked = false;
        //行业标准
        this.hybzCheckBox.Checked = false;
        //考察报告
        this.kcbgCheckBox.Checked = false;
        //国研报告
        this.gybgCheckBox.Checked = false;
        //科技报告
        this.kjbgCheckBox.Checked = false;
        //能源报告
        this.nybgCheckBox.Checked = false;
        //学位论文
        this.xwlwCheckBox.Checked = false;
        //中国成果
        this.zgcgCheckBox.Checked = false;
        //科技成果
        this.kjcgCheckBox.Checked = false;
        //自产文献
        this.zcwxCheckBox.Checked = false;
        //电力报刊
        this.dlbkCheckBox.Checked = false;
        //科技报刊
        this.kjbkCheckBox.Checked = false;
        //经济报刊
        this.jjbkcheckBox.Checked = false;
        //综合报刊
        this.zhbkcheckBox.Checked = false;
        //图书手册
        this.tsscCheckBox.Checked = false;
    }
    /// <summary>
    /// 设置具体的一个功能信息
    /// </summary>
    /// <param name="temp">要设置的功能模块</param>
    private void SetFuncion(FunctionModular temp)
    {
        //中文期刊
        if (temp.id.Equals("zhongwenqikan"))
        {
            this.zwqkCheckBox.Checked = true;
            return;
        }
        //外文期刊
        if (temp.id.Equals("waiwenqikan"))
        {
            this.wwqkCheckBox.Checked = true;
            return;
        }
        //中文会议
        if (temp.id.Equals("zhongwenhuiyi"))
        {
            this.zwhyCheckBox.Checked = true;
            return;
        }
        //外文会议
        if (temp.id.Equals("waiwenhuiyi"))
        {
            this.wwhyCheckBox.Checked = true;
            return;
        }
        //国电法规
        if (temp.id.Equals("guodianfagui"))
        {
            this.gdfgCheckBox.Checked = true;
            return;
        }
        //国家标准
        if (temp.id.Equals("guojiabiaozhun"))
        {
            this.gjbzCheckBox.Checked = true;
            return;
        }
        //国际标准
        if (temp.id.Equals("guojibiaozhun"))
        {
            this.gjibzcheckBox.Checked = true;
            return;
        }
        //行业标准
        if (temp.id.Equals("hangyebiaozhun"))
        {
            this.hybzCheckBox.Checked = true;
            return;
        }
        //考察报告
        if (temp.id.Equals("kaochabaogao"))
        {
            this.kcbgCheckBox.Checked = true;
            return;
        }
        //国研报告
        if (temp.id.Equals("guoyanbaogao"))
        {
            this.gybgCheckBox.Checked = true;
            return;
        }
        //科技报告
        if (temp.id.Equals("kejibaogao"))
        {
            this.kjbgCheckBox.Checked = true;
            return;
        }
        //能源报告
        if (temp.id.Equals("nengyuanbaogao"))
        {
            this.nybgCheckBox.Checked = true;
            return;
        }
        //学位论文
        if (temp.id.Equals("xueweilunwen"))
        {
            this.xwlwCheckBox.Checked = true;
            return;
        }
        //中国成果
        if (temp.id.Equals("zhongguochengguo"))
        {
            this.zgcgCheckBox.Checked = true;
            return;
        }
        //科技成果
        if (temp.id.Equals("kejichengguo"))
        {
            this.kjcgCheckBox.Checked = true;
            return;
        }
        //自产文献
        if (temp.id.Equals("zichanwenxian"))
        {
            this.zcwxCheckBox.Checked = true;
            return;
        }
        //电力报刊
        if (temp.id.Equals("dianlibaokan"))
        {
            this.dlbkCheckBox.Checked = true;
            return;
        }
        //科技报刊
        if (temp.id.Equals("kejibaokan"))
        {
            this.kjbkCheckBox.Checked = true;
            return;
        }
        //经济报刊
        if (temp.id.Equals("jingjibaokan"))
        {
            this.jjbkcheckBox.Checked = true;
            return;
        }
        //综合报刊
        if (temp.id.Equals("zonghebaokan"))
        {
            this.zhbkcheckBox.Checked = true;
            return;
        }
        //图书手册
        if (temp.id.Equals("tushushouce"))
        {
            this.tsscCheckBox.Checked = true;
            return;
        }
    }

    /// <summary>
    /// 设置角色
    /// </summary>
    private void SetRole()
    {
        if (this.user.role == UserRole.NORMAL)
        {
            this.roleRadioButton.Checked = true;
        }
        else
        {
            this.adminRadioButton.Checked = true;
        }
    }


    /// <summary>
    /// 更新用户信息
    /// </summary>
    private void UpdateUser()
    {
        //生成user对象
        this.user = new User();
        this.user.name = this.unameTextBox.Text.Trim();
        //设置用户密码
        this.user.password = this.upwdTextBox.Text.Trim();

        //设置IP
        this.user.ipLimit = this.IPTextBox.Text.Trim();

        //设置用户角色
        if (this.adminRadioButton.Checked)
        {
            this.user.role = UserRole.EDITOR;
        }
        else
        {
            this.user.role = UserRole.NORMAL;
        }

        //设置用户功能模块
        this.user.grantedModulars = GetFunctionModulars();

        //更新用户信息
        this.userDAO.UpdateUserByName(user);
    }

    /// <summary>
    /// 获取选择的用户功能模块列表
    /// </summary>
    /// <returns></returns>
    public IList<FunctionModular> GetFunctionModulars()
    {
        IList<FunctionModular> result = new List<FunctionModular>();
        FunctionModular temp = null;

        //中文期刊功能
        if (this.zwqkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongwenqikan";
            result.Add(temp);
        }

        //外文期刊功能
        if (this.wwqkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "waiwenqikan";
            result.Add(temp);
        }

        //中文会议功能
        if (this.zwhyCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongwenhuiyi";
            result.Add(temp);
        }

        //外文会议
        if (this.wwhyCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "waiwenhuiyi";
            result.Add(temp);
        }

        //国电法规
        if (this.gdfgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guodianfagui";
            result.Add(temp);
        }

        //国家标准
        if (this.gjbzCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guojiabiaozhun";
            result.Add(temp);
        }

        //国际标准
        if (this.gjibzcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guojibiaozhun";
            result.Add(temp);
        }
        //行业标准
        if (this.hybzCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "hangyebiaozhun";
            result.Add(temp);
        }
        //考察报告
        if (this.kcbgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kaochabaogao";
            result.Add(temp);
        }
        //国研报告
        if (this.gybgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "guoyanbaogao";
            result.Add(temp);
        }
        //科技报告
        if (this.kjbgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejibaogao";
            result.Add(temp);
        }
        //能源报告
        if (this.nybgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "nengyuanbaogao";
            result.Add(temp);
        }
        //学位论文
        if (this.xwlwCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "xueweilunwen";
            result.Add(temp);
        }

        //中国成果
        if (this.zgcgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zhongguochengguo";
            result.Add(temp);
        }
        //科技成果
        if (this.kjcgCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejichengguo";
            result.Add(temp);
        }
        //自产文献
        if (this.zcwxCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zichanwenxian";
            result.Add(temp);
        }
        //电力报刊
        if (this.dlbkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "dianlibaokan";
            result.Add(temp);
        }
        //科技报刊
        if (this.kjbkCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "kejibaokan";
            result.Add(temp);
        }

        //经济报刊
        if (this.jjbkcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "jingjibaokan";
            result.Add(temp);
        }
        //综合报刊
        if (this.zhbkcheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "zonghebaokan";
            result.Add(temp);
        }
        //图书手册
        if (this.tsscCheckBox.Checked)
        {
            temp = new FunctionModular();
            temp.id = "tushushouce";
            result.Add(temp);
        }

        return result;
    }

    /// <summary>
    /// 恢复用户信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetBtn_Click(object sender, EventArgs e)
    {
        BindData();
    }
    
    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitBtn_Click(object sender, EventArgs e)
    {
        UpdateUser();
        Response.Redirect(GlobalUser.LISTUSER_PAGE);
    }

   
}